package com.chj.resources;

import java.util.Enumeration;

import javax.servlet.http.HttpServletRequest;

import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/api")
public class TestResource {

//	@Autowired
//	RestTemplate restTemplate;

	@GetMapping("/test")
	public String test() {
		return "test --> ";
	}

	// @PreAuthorize("hasRole('select')")
	@GetMapping("/test1")
	public String test1(@RequestParam("p") String param, HttpServletRequest request) {
		printHeader(request);
		String loginUser = (String) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		String param1 = param + " --> " + "TestResource01.test1 --> " + loginUser;
		return param1;
//		String url = "http://localhost:9102/test-resource02/api/test1?p=" + param1;
//		return restTemplate.getForEntity(url, String.class).getBody();
	}

	// @PreAuthorize("hasRole('update')")
	@GetMapping("/test2")
	public String test2(@RequestParam("p") String param, HttpServletRequest request) {
		printHeader(request);
		String loginUser = (String) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		String param1 = param + " --> " + "TestResource01.test2 --> " + loginUser;
//		String url = "http://localhost:9102/test-resource02/api/test2?p=" + param1;
//		return restTemplate.getForEntity(url, String.class).getBody();
		return param1;
	}
	
	private void printHeader(HttpServletRequest request){
		Enumeration<String> names = request.getHeaderNames();
		while(names.hasMoreElements()){
			String name = names.nextElement();
			System.out.println(name + "  --->  " + request.getHeader(name));
		}
		
	}
}
